package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.MyPageResult;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.mapper.YddUserMapper;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.YddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {
    @Autowired
    YddUserMapper yddUserMapper;
    @Autowired
    ArticleService articleService;

    @Override
    public HashMap<String, Object> getPageHashMap(String sex, String username, String mobile, Integer page, Integer limit) {
        Page<YddUser> catePage = new Page<>(page, limit);
        LambdaQueryWrapper<YddUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(sex != null && !sex.equals(""), YddUser::getSex, sex).
                like(username != null, YddUser::getUsername, username).
                like(mobile != null, YddUser::getMobile, mobile);
        this.page(catePage, lqw);
        List<YddUser> records = catePage.getRecords();
        Integer count = this.count(lqw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("currentPage", page);
        map.put("pageSize", limit);
        map.put("totalPages", catePage.getTotal());
        map.put("data", records);
        return map;
    }

    /**
     * Description:
     * date: 2022/3/3 11:38
     *
     * @author: chp
     * @since JDK 1.8
     */
    @Override
    public MyPageResult userDataList(Integer type, Integer userId, Page page) {


        Integer[] articleIds = yddUserMapper.getArticleIds(userId, type);

        if (articleIds.length == 0) {
            return null;
        }

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Article::getId, articleIds);

        Page pageInfo = articleService.page(page, queryWrapper);


        MyPageResult init = MyPageResult.init(pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getCurrent(), pageInfo.getRecords());

        return init;
    }

    @Override
    public void addUserDataHandle(Integer type, Integer article_id, Integer userId) {
        yddUserMapper.save(type, article_id, userId);
        Article article = articleService.getById(article_id);
        article.setLovenum(article.getLovenum() + 1);
        articleService.updateById(article);
    }

    @Override
    public void delUserDataHandle(Integer type, Integer article_id, Integer userId) {
        yddUserMapper.del(type, article_id, userId);
        Article article = articleService.getById(article_id);
        article.setLovenum(article.getLovenum() - 1);
        articleService.updateById(article);
    }
}
